package com.lanou.weekend02.controller;

import com.lanou.weekend02.bean.AjaxResult;
import com.lanou.weekend02.bean.User;
import com.lanou.weekend02.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author lizhongren1.
 */
@RestController
@CrossOrigin
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/isexists")
    public AjaxResult isUserNameExists(@RequestParam("username") String uname){

        boolean b = userService.judgeUsername(uname);
        if (!b){
            return new AjaxResult("用户名不符合要求",1002,uname);
        }

        boolean exists = userService.isUserExists(uname);
        if (exists){
            return new AjaxResult("用户名已存在",1003,uname);
        }
        return new AjaxResult("用户名可以使用",1000,uname);
    }

    @RequestMapping(value = "/reg", method = RequestMethod.POST)
    public AjaxResult registerUser(User user){

        return userService.judgeUserInfo(user);
    }

    @RequestMapping("/login")
    public AjaxResult login(User user){
        System.out.println(user);

        if (userService.login(user)){
            return new AjaxResult("登录成功!", 1000, user);
        }
        return new AjaxResult("用户名或者密码不正确", 1007,null);
    }



}
